﻿using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Data;
using System.Data.SqlClient;

/// <summary>
/// GetTickets 的摘要说明
/// </summary>
[WebService(Namespace = "http://skypace.51vip.biz:811/crsi")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Tickets : System.Web.Services.WebService {

    public Tickets () {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    //[WebMethod]
    //public DataSet GetTickets(long order_no,string user,string pwd) //此结果集都是城市代码，可否用receipt取代？
    //{
    //    SqlCommand cmd = new SqlCommand();
    //    SqlParameter para;
    //    DataSet ds = new DataSet();
    //    try
    //    {
    //        cmd.CommandText = "crsi_get_order_tickets";
    //        para = cmd.Parameters.AddWithValue("@bk_id", order_no);
    //        para = cmd.Parameters.AddWithValue("@user", user);
    //        para = cmd.Parameters.AddWithValue("@pwd", password);
    //        //para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
    //        ret_code = TinyDAL.Exec(ref cmd, ref ds);
    //        ret_msg = "";
    //    }
    //    catch (Exception ex)
    //    {
    //        ret_msg = ex.Message;
    //        ret_code = -1;
    //    }
    //    return ds;
    //}

    //[WebMethod]
    //public string ChangePassenger()//通常不允许修改姓名，所以CRSI不提供此接口！
    //{
    //    return "Hello World";
    //}

    [WebMethod]
    public int Cancel(long order_no, int pax_sn, string user, string sign, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        int ret_code;
        DataTable dt = new DataTable();
        string office;

        if (user.Length > 20 || user.Length < 1 || sign.Length > 50)
        {
            ret_msg = "Found invalid parameters!";
            return -1;
        }
        string sign_key = order_no.ToString() + "|" + pax_sn.ToString();
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ret_code;
        }


        //try
        //{
        //    cmd.CommandText = "crsi_get_user_pwd_hash";
        //    para = cmd.Parameters.AddWithValue("@uid", user);
        //    ret_code = TinyDAL.Exec(ref cmd, ref dt);
        //    ret_msg = "";
        //}
        //catch (Exception ex)
        //{
        //    ret_msg = ex.Message;
        //    ret_code = -1;
        //    return ret_code;
        //}
        //if (dt.Rows.Count != 1)
        //{
        //    ret_msg = "User Not found!";
        //    ret_code = -1;
        //    return ret_code;
        //}
        //string inbuf = order_no.ToString() + "|" + pax_sn.ToString() + "|" + dt.Rows[0][0].ToString();
        //string outbuf = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(inbuf, "MD5");
        //if (outbuf != sign)
        //{
        //    ret_msg = "Illegal!";
        //    ret_code = -1;
        //    return ret_code;
        //}


        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "crsi_cancel_ticket";
            para = cmd.Parameters.AddWithValue("@bk_id", order_no);
            para = cmd.Parameters.AddWithValue("@pax_sn",pax_sn);
            //para = cmd.Parameters.AddWithValue("@cancel_type", "P");
            para = cmd.Parameters.AddWithValue("@user", user);
            //para = cmd.Parameters.AddWithValue("@pwd", pwd);
            para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
        }
        return ret_code;
    }

    [WebMethod]
    public DataSet OrderReceipts(long order_no, int lang, string user, string sign, out int ret_code,out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ds;
        }
        string sign_key = order_no.ToString();
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ds;
        }

        
        //SqlCommand cmd = new SqlCommand();
        //SqlParameter para;
        //DataSet ds = new DataSet();
        //DataTable dt = new DataTable();
        //try
        //{
        //    cmd.CommandText = "crsi_get_user_pwd_hash";
        //    para = cmd.Parameters.AddWithValue("@uid", user);
        //    ret_code = TinyDAL.Exec(ref cmd, ref dt);
        //    ret_msg = "";
        //}
        //catch (Exception ex)
        //{
        //    ret_msg = ex.Message;
        //    ret_code = -1;
        //    return ds;
        //}
        //if (dt.Rows.Count != 1)
        //{
        //    ret_msg = "User Not found!";
        //    ret_code = -1;
        //    return ds;
        //}
        //string inbuf = order_no.ToString() + "|"  + dt.Rows[0][0].ToString();
        //string outbuf = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(inbuf, "MD5");
        //if (outbuf != sign)
        //{
        //    ret_msg = "Illegal!";
        //    ret_code = -1;
        //    return ds;
        //}


        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "crsi_get_receipts_by_order";
            para = cmd.Parameters.AddWithValue("@bk_id", order_no);
            para = cmd.Parameters.AddWithValue("@lang", lang);
            para = cmd.Parameters.AddWithValue("@user", user);
            //para = cmd.Parameters.AddWithValue("@pwd", pwd);
            //para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd, ref ds);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
        }
        return ds;
    }

    [WebMethod]
    public DataSet GetEmdReceipt(long order_no, short pax_sn, string user, string sign, out int ret_code, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ds;
        }
        string sign_key = order_no.ToString() + "|" + pax_sn.ToString();
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ds;
        }

        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "crsi_get_tkt_emd_receipt";
            para = cmd.Parameters.AddWithValue("@bk_id", order_no);
            para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
            para = cmd.Parameters.AddWithValue("@user", user);
            //para = cmd.Parameters.AddWithValue("@pwd", pwd);
            //para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd, ref ds);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
        }
        return ds;
    }

    [WebMethod]
    public DataSet VerifyTicket(long order_no, int pax_sn,/*string family_name,string name,*/string id_no,int lang, string user, string sign, out int ret_code, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ds;
        }
        string sign_key = order_no.ToString() + "|" + pax_sn.ToString();
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ds;
        }

        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "crsi_verify_ticket";
            para = cmd.Parameters.AddWithValue("@bk_id", order_no);
            para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
            //para = cmd.Parameters.AddWithValue("@family_name", family_name);
            //para = cmd.Parameters.AddWithValue("@name", name);
            para = cmd.Parameters.AddWithValue("@id_no", id_no);
            para = cmd.Parameters.AddWithValue("@lang", lang);
            para = cmd.Parameters.AddWithValue("@user", user);
            //para = cmd.Parameters.AddWithValue("@pwd", pwd);
            //para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd, ref ds);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
        }
        return ds;
    }

    [WebMethod]
    public int TripCheckIn(long order_no, int pax_sn, int trip_sn, int row, string col, string user, string sign, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        int ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ret_code;
        }
        string sign_key = order_no.ToString() + "|" + pax_sn.ToString() + "|" + trip_sn.ToString() + "|" + row.ToString() + "|" + col;
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ret_code;
        }

        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "crsi_check_in";
            para = cmd.Parameters.AddWithValue("@bk_id", order_no);
            para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
            para = cmd.Parameters.AddWithValue("@trip_sn", trip_sn);
            para = cmd.Parameters.AddWithValue("@row", row);
            para = cmd.Parameters.AddWithValue("@col", col);
            para = cmd.Parameters.AddWithValue("@user", user);
            //para = cmd.Parameters.AddWithValue("@pwd", pwd);
            para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
        }
        return ret_code;
    }

    [WebMethod]
    public DataSet GetTripSeatMap(long order_no, int pax_sn,int trip_sn, string user, string sign, out int ret_code, out string ret_msg)
    {
        SqlCommand cmd = new SqlCommand();
        SqlParameter para;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        string office;
        ret_code = 0;
        ret_msg = "";
        if (user.Length > 20 || user.Length < 1 || sign.Length > 50)
        {
            ret_msg = "Found invalid parameters!";
            ret_code = -1;
            return ds;
        }
        string sign_key = order_no.ToString() + "|" + pax_sn.ToString() + "|" + trip_sn.ToString();
        ret_code = API_Auth.Check(user, sign_key, sign, out office);
        if (ret_code != 0)
        {
            ret_msg = "Check API authorize failed!";
            return ds;
        }

        
        cmd.Parameters.Clear();
        try
        {
            cmd.CommandText = "crsi_get_flt_avail_seat_map";
            para = cmd.Parameters.AddWithValue("@bk_id", order_no);
            para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
            para = cmd.Parameters.AddWithValue("@trip_sn", trip_sn);
            para = cmd.Parameters.AddWithValue("@user", user);
            //para = cmd.Parameters.AddWithValue("@pwd", pwd);
            //para = cmd.Parameters.AddWithValue("@ip_addr", HttpContext.Current.Request.UserHostAddress);
            ret_code = TinyDAL.Exec(ref cmd, ref ds);
            ret_msg = "";
        }
        catch (Exception ex)
        {
            ret_msg = ex.Message;
            ret_code = -1;
        }
        return ds;
    }
    
}

